/**
 * Copyright &copy; 2012-2015 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.soccer.cms.modules.cms.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.soccer.cms.common.config.Global;
import com.soccer.cms.common.persistence.Page;
import com.soccer.cms.common.web.BaseController;
import com.soccer.cms.common.utils.StringUtils;
import com.soccer.cms.modules.cms.entity.Club;
import com.soccer.cms.modules.cms.service.ClubService;

/**
 * 俱乐部Controller
 * @author zouqh
 * @version 2015-05-25
 */
@SuppressWarnings("unused")
@Controller
@RequestMapping(value = "${adminPath}/cms/club")
public class ClubController extends BaseController {

	@Autowired
	private ClubService clubService;
	
	@ModelAttribute
	public Club get(@RequestParam(required=false) String id) {
		Club entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = clubService.get(id);
		}
		if (entity == null){
			entity = new Club();
		}
		return entity;
	}
	
	@RequiresPermissions("cms:club:view")
	@RequestMapping(value = {"index"})
	public String indexPage(Club club, HttpServletRequest request, HttpServletResponse response, Model model) {
		
		return "modules/cms/clubIndex";
	}
	
	@RequiresPermissions("cms:club:view")
	@RequestMapping(value = {"list", ""})
	public String list(Club club, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Club> page = clubService.findPage(new Page<Club>(request, response), club); 
		model.addAttribute("page", page);
		return "modules/cms/clubList";
	}

	@RequiresPermissions("cms:club:view")
	@RequestMapping(value = "form")
	public String form(Club club, Model model) {
		model.addAttribute("club", club);
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		model.addAttribute("opt",request.getParameter("opt"));
		return "modules/cms/clubForm";
	}

	@RequiresPermissions("cms:club:edit")
	@RequestMapping(value = "save")
	public String save(Club club, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, club)){
			return form(club, model);
		}
		clubService.save(club);
		//addMessage(redirectAttributes, "保存俱乐部成功");
		//return "redirect:"+Global.getAdminPath()+"/cms/club/?repage";
		model.addAttribute("club", club);
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		model.addAttribute("opt","save");
		model.addAttribute("message","保存俱乐部成功");
		return "modules/cms/clubForm";
	}
	
	@RequiresPermissions("cms:club:edit")
	@RequestMapping(value = "delete")
	public String delete(Club club, RedirectAttributes redirectAttributes) {
		clubService.delete(club);
		addMessage(redirectAttributes, "删除俱乐部成功");
		return "redirect:"+Global.getAdminPath()+"/cms/club/?repage";
	}

	@RequiresPermissions("cms:club:edit")
	@RequestMapping(value = "batchDelete")
	public String delete(String ids, RedirectAttributes redirectAttributes) {
		clubService.delete(ids);
		addMessage(redirectAttributes, "删除俱乐部成功");
		return "redirect:"+Global.getAdminPath()+"/cms/club/?repage";
	}
}